package com.woniuxy.main.chat_yj.controller;

import com.woniuxy.main.chat_yj.model.User;
import com.woniuxy.main.chat_yj.service.UserService_Yj;
import com.woniuxy.main.mycenter_ty.pojo.User_Ty;
import com.woniuxy.main.mycenter_ty.service.MyCenterService;
import com.woniuxy.main.utils.ResponseResult;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * 聊天controller
 */
@Controller
public class LoginController {

    @Autowired
    private UserService_Yj userService;
	@Autowired
	private MyCenterService personCenterservice;
    
	
    /**
     * 登陆界面
     */
    @GetMapping("/")
    public ModelAndView login() {
        return new ModelAndView("login");
    }
    

    /**
     * 聊天界面
     */
    @GetMapping("/index2")
    public ModelAndView index2(HttpServletRequest request) throws UnknownHostException {
    	String phone = (String)SecurityUtils.getSubject().getPrincipal();  // 获取当前登录用户
    	User_Ty us = personCenterservice.queryAllInfoByPhone(phone);   // 根据手机号查询个人信息
    	String username = us.getUser_name();    // 获取用户名（英文） 	
    	ModelAndView mav = new ModelAndView("chat2");
        if (username==null) {
            username = "匿名用户";
        }
        User user = userService.getUserByName(username);
        mav.addObject("username", user.getUsername());
        mav.addObject("name", user.getName());
        mav.addObject("userId", user.getId());
        mav.addObject("webSocketUrl", "ws://"+ InetAddress.getLocalHost().getHostAddress()+":"+request.getServerPort()+request.getContextPath()+"/chat");
        return mav;
    }

    

    /**
     * 登录
     */
    @RequestMapping("/dologin")
    @ResponseBody
    public ResponseResult dologin(@RequestBody User currUser, HttpServletRequest request,HttpSession session) throws UnknownHostException {
        ModelAndView mav = new ModelAndView("chat2");
        /*if (StringUtils.isEmpty(currUser.getUsername())) {
            currUser.getUsername() = "匿名用户";
        }*/
        User user = userService.getUserByName(currUser.getUsername());
        if(user != null){
            if(user.getPassword().equals(currUser.getPassword())){
                System.out.println("系统用户登录...."+currUser.getUsername());
                mav.addObject("username", user.getName());
            }else {
                System.out.println("系统用户登录....账号密码不对！！！"+currUser.getUsername());
                return ResponseResult.build(400,"账号密码不对！！！");
            }
        }else {
            System.out.println("你还没注册，赶紧注册一个玩玩吧....！！！"+currUser.getUsername());
            return ResponseResult.build(400,"你还没注册，赶紧注册一个玩玩吧！！！");
        }
        session.setAttribute("userInfo",user);
        User userInfo = (User) session.getAttribute("userInfo");
        String username = userInfo.getUsername();
        mav.addObject("webSocketUrl", "ws://"+ InetAddress.getLocalHost().getHostAddress()+":"+request.getServerPort()+request.getContextPath()+"/chat");
        return ResponseResult.build(200,"登录成功");
    }
   
    
}
